package com.dietdroid;

import java.util.ArrayList;

import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class NutritionFoodHistoryActivity extends ListActivity {

	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.nutrition_food_history);
        
        int year = this.getIntent().getIntExtra("YEAR", -1);
        int month = this.getIntent().getIntExtra("MONTH", -1);
        int day = this.getIntent().getIntExtra("DAY", -1);
        String nutrition_fact = this.getIntent().getStringExtra("NUTRITION_FACT");

        final ArrayList<NutritionFoodHistoryModel> nutrition_data = DietDataOperations.getNutritionFactFoods(month, day, year, nutrition_fact, this.getApplicationContext());
        if(nutrition_data == null) {
        	this.setListAdapter(null);
        }else {
        	this.setListAdapter(new NutritionFoodAdapter(nutrition_data));
        }
        
 
	}
	
	private class NutritionFoodAdapter extends ArrayAdapter<NutritionFoodHistoryModel>{
		
		private ArrayList<NutritionFoodHistoryModel> history_list;

		public NutritionFoodAdapter(
				ArrayList<NutritionFoodHistoryModel> history_list) {
			super(NutritionFoodHistoryActivity.this, R.layout.nutrition_food_history_row, history_list);
			this.history_list = history_list;
			
		}
		
	    @Override
	    public View getView(int position, View convertView, ViewGroup parent) {
	    	View v = convertView;
	    	if (v == null) {
	    		LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	    		v = vi.inflate(R.layout.nutrition_food_history_row, parent, false);
	    	}
	    	NutritionFoodHistoryModel model = history_list.get(position);
	    	TextView tt = (TextView) v.findViewById(R.id.nutrition_food_name_txt);
	    	TextView bt = (TextView) v.findViewById(R.id.nutrition_food_percent_txt);
	    	tt.setText(model.getFood_name());                            
	    	bt.setText(String.valueOf(model.getFood_percent()) + "%");
	    	return v;
	    }
	}

}
